package com.shop.storage.sync;

import com.shop.storage.domain.WmsForewarning;
import com.shop.storage.domain.WmsWareSku;
import com.shop.storage.service.IWmsForewarningService;
import com.shop.storage.service.IWmsWareSkuService;
import com.shop.storage.vo.response.ForewarningResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PurchaseSync
 * @Description 描述
 * @Author Wen.Li
 * @Date 2024/6/24 14:07
 */
@Component
@Log4j2
public class WareSkuSync {
    @Autowired
    private IWmsWareSkuService wareSkuService;

    @Autowired
    private IWmsForewarningService forewarningService;



    @Scheduled(cron = "0 0 0 0/1 * ?")
    @Transactional
    public void wareSkuSync(){
        log.info("开始筛选");
        //调用接口查询全部数据
        List<WmsWareSku> wareSkus = wareSkuService.selectWmsWareSkuList(null);
        //循环次集合
        wareSkus.forEach(item->{
            // 筛选
            if (item.getForewarningSum()>=item.getStock()){
                //查询预警列表
                List<ForewarningResponse> wmsForewarnings = forewarningService.selectWmsForewarningList(null);
                //循环已存在的
                wmsForewarnings.forEach(itemFprewarning->{
                    //判断预警表内不存在 或者 该数据存在但状态是之前已完成的数据  预警添加次数据
                    if (!itemFprewarning.getWareSkuId().equals(item.getId()) ||
                            itemFprewarning.getWareSkuId().equals(item.getId()) && itemFprewarning.getStatus() == 5){
                        //添加
                        WmsForewarning forewarningInsert = new WmsForewarning();
                        forewarningInsert.setWareSkuId(item.getId());
                        forewarningService.insertWmsForewarning(forewarningInsert);
                    }
                });
            }
        });



    }
}
